package com.example.demo.util;


 import com.rabbitmq.client.Channel;
 import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Service;

 import java.io.IOException;
 import java.util.Random;
 import java.util.concurrent.TimeUnit;

/**
 * Created by zhangting on 31/03/2021.
 */
@Service
@Slf4j
public class ReceivedMsg {

    @RabbitListener(queues = "atopic.queue.a")
    public void senda(String msg, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG)
                      long tag) throws InterruptedException, IOException {
      log.info("CC1: received msg :{},channel :{},deliverytag:{}",msg,channel.toString(),tag);

        //TimeUnit.MILLISECONDS.sleep(new Random().nextInt(30));
        //channel.basicAck(tag,false);

       /* channel.basicAck(tag,false);
        if(msg.contains("10")){
            channel.basicNack(tag,false,false);
            //第二个参数是否应用于多消息，第三个参数是否requeue
        }if(msg.contains("11")){
            //channel.basicNack(tag,false,true);
            //第二个参数是否应用于多消息，第三个参数是否requeue
        }else if(msg.contains("20")){
            channel.basicReject(tag,false);//第二个参数是否requeue，true则重新入队列，否则丢弃或者进入死信队列。
        }*/

        log.info("CC2: received msg : ");
    }



    @RabbitListener(queues = "btopic.queue.b")
    public void sendb(String msg, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG)
            long tag) throws InterruptedException, IOException {
        log.info("BBB1:  received msg :{},channel :{},deliverytag:{}",msg,channel.toString(),tag);
        //channel.basicAck(tag,false);
        //TimeUnit.MILLISECONDS.sleep(new Random().nextInt(10));

        /*if(msg.contains("10")){
            channel.basicNack(tag,false,false);
            //第二个参数是否应用于多消息，第三个参数是否requeue
        }if(msg.contains("11")){
            channel.basicNack(tag,false,false);
            //第二个参数是否应用于多消息，第三个参数是否requeue
        }else if(msg.contains("20")){
            channel.basicReject(tag,false);//第二个参数是否requeue，true则重新入队列，否则丢弃或者进入死信队列。
        }
        channel.basicAck(tag,false);*/

        log.info("BBB2: received msg  " );

    }

}
